import React, { Component } from 'react'
import ReactDOM from 'react-dom';
import { Modal, Form } from 'antd';
import { ModalForm } from '../../components'
import { fetchData } from '../../components/AjaxConsumer'
const confirm = Modal.confirm;

const WrappedAdvancedSearchForm = Form.create()(ModalForm);

export default class UpdateUserPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            resData: null,
            submitUrl:'/base/auth/user/c'
        };

        this.formAlerSize = 'large'; //弹出框的大小 只有'large'和'small'两种值 （必须,输错时默认为large）
        this.getFormData = [
            {
                type : 'hiddenInput',
                showName : 'id',
                name : 'userId',
                initialValue : ''
            },
            {
                type : 'input',
                showName : '登录用户名',
                name : 'loginName',
                initialValue : '',
                required:'required'  //选填
            },{
                type : 'select',
                showName : '是否可登陆',
                name : 'loginFlag',
                optionList:[              //必填  下拉框的信息
                    {
                        optionKey:'1',
                        optionShow:'是'
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    }
                ],
                initialValue : '1',
                required: true
            },{
                type : 'input',
                showName : '人员姓名',
                name : 'userName',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '邮箱',
                name : 'email',
                initialValue : ''
            },{
                type : 'input',
                showName : '座机号',
                name : 'phone',
                initialValue : ''
            },{
                type : 'input',
                showName : '手机号',
                name : 'mobile',
                initialValue : ''
            },{
                type : 'dataTimePicker',
                showName : '入职时间',
                name : 'hireDate',
                initialValue : ''
            },{
                type : 'dataTimePicker',
                showName : '离职时间',
                name : 'leaveDate',
                initialValue : ''
            },{
                type : 'select',
                showName : '在职状态',
                name : 'hireFlag',
                optionList:[              //必填  下拉框的信息
                    {
                        optionKey:'1',
                        optionShow:'在职'
                    },{
                        optionKey:'0',
                        optionShow:'离职'
                    }
                ],
                initialValue : '1',
                required: true
            },{
                type : 'textarea',
                showName : '备注',
                name : 'remark',
                initialValue : ''
            }
        ];
    }
    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    // 隐藏弹出框
    hideModal = () => {
        this.setState({
            visible: false,
        });
    }
    // 新增用户
    addUserHandle = () => {
        this.setState({
            submitUrl:'/base/auth/user/c',
            formTitle: '新增用户',
            resData: null
        });
        //清空input值
        // this.getFormData.map( (e, index) => {
        //     e.initialValue = ''
        // });
        this.showModal();//显示弹框
    }

    editGetInfoCallBack = (resData) =>{
        this.setState({
            submitUrl:'/base/auth/user/u',
            resData: resData
        });
        this.showModal();//显示弹框
    }

    // 编辑信息
    editHandle = (rowObj) => {
        this.setState({
            resData: null,
            formTitle: '修改用户'
        });
        //根据对象的Id 属性获取对象信息
        let userId = rowObj.userId;
        const options= {
            url: '/base/auth/user/r/'+userId,
            condition:{},
        };
        fetchData(this, options, this.editGetInfoCallBack);
    }

    // 删除用户
    deleteHandle = (rowObj) => {
        let userId = rowObj.userId;
        // let userIdArr = [];
        // userIdArr.push(userId);
        const options= {
            url: '/base/auth/user/d',
            condition:{userIds:userId},
            contentType:'application/x-www-form-urlencoded'
        };
        let _that = this;
        confirm({
            title: '您确定要删除吗？',
            onOk() {
                fetchData(_that, options, (resData) => {
                    _that.setState({
                        resData: resData
                    });
                    if (resData.code == '0'){
                        alert(resData.msg);
                    }else {
                        _that.refreshHandle();
                    }
                });
            },
            onCancel() {
                console.log("取消了删除操作")
            },
        });
    }

    // 新增/修改后的回调
    handleOk = () => {
        // let result = this.state.resData;
        this.hideModal();
        this.refreshHandle();
    }

    handleCancel = () => {
        this.hideModal();
    }


    //刷新List列表
    refreshHandle = () => {
        console.log("刷新List列表")
        this.props.refreshHandle();
    }

    componentDidMount() {

    }

    render() {

        if(this.state.resData != null &&  this.state.resData.data != null){
            let user = this.state.resData.data;
            this.getFormData.map( (e, index) => {
                e.initialValue = user[e.name]
            });
        } else {
            this.getFormData.map( (e, index) => {
                if(e.type == 'select'){
                    e.initialValue = '1'
                } 
                // else if (e.type == 'loginName'){
                //     e.disable = true;
                // } 
                else {
                    e.initialValue = ''
                }
            });
        }

        return(
            <Modal
                style={{top:'20px'}}
                visible={this.state.visible}
                title={this.state.formTitle ? this.state.formTitle : '填写表单'}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={this.formAlerSize=='large'?'850px':'400px'}
                footer=''
            >

                <WrappedAdvancedSearchForm
                    ref="modalFrom"
                    title="用户信息"
                    formAlerSize={this.formAlerSize}
                    formUrl={this.state.submitUrl}
                    getFormData={this.getFormData}
                    closeBox={this.handleCancel}
                    checkBox={this.handleOk} />

            </Modal>
        )
    }
}
